﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using wpf =System.Windows.Controls ;
using AvalonDock;
using System.Windows.Forms;
using DataLayer;
using System.IO;
namespace LuanVanProject
{
    /// <summary>
    /// Interaction logic for ProjectPane.xaml
    /// </summary>
    public partial class ProjectPane : System.Windows.Controls.UserControl,Observer
    {
        public bool IsGlobal;
        public class MindmapTag
        {
            public MindMap mindmap;
            public MindmapEditor editor;
            public string path;
            public DocumentContainForViewer docContent;
        }
        void Observer.Update()
        {
            PrepareNodeTree();
        }
        public void SetMindmapAndOutputInvisible()
        {
            Step3.Visibility = Visibility.Collapsed;
            Step4.Visibility = Visibility.Collapsed;
        }
        public System.Windows.Controls.TreeView _NodeTree;
        public MindmapEditor editor;
        
        string SelectedImagePath;
        private object dummyNode = null;
        
        public ProjectPane()
        {
            InitializeComponent();
            _NodeTree = NodeTree;
            TreeViewItem item = new TreeViewItem();
            item.Items.Add("Add a Document");
            item.MouseUp += new MouseButtonEventHandler(Add_File_Event);
            this.foldersItem.Items.Clear();
            this.foldersItem.Items.Add(item);
        }
       
        private void Context_MenuItem_Delete_Function_Click(object sender, RoutedEventArgs e)
        {

        }
        /*
        public void SaveCurrentTab()
        {
            if (foldersItem.Items != null && foldersItem.Items.Count != 0)
                foreach (TreeViewItem s in foldersItem.Items)
                {
                    TreeItemHandler handler = s.Tag as TreeItemHandler;
                    string selectedPath = "";
                    if (DockPane.SelectedItem != null)
                    {
                        selectedPath = (DockPane.SelectedItem as DocumentContainForViewer).FileName;
                    }
                    if (handler.DocContent != null)
                    {
                        MyWindow myWin = new MyWindow();
                        myWin.ContentId = ResourcesHelper.GetInstance().PathToID(handler.FileName);
                        if (handler.FileName == selectedPath)
                            myWin.Status = true;
                        else
                        {
                            myWin.Status = false;
                        }
                        WindowsHelper.GetInstance().AddWindows(myWin);
                    }
                }
        }*/
       
        private void Add_File_Event(object sender, RoutedEventArgs e)
        {
            OpenFileDialog sfd = new OpenFileDialog();
            
            sfd.Title = "Add New File";
            sfd.Filter = "Supported Files(PDF;DOC;DOCX;PPTX)|*.PDF;*.DOCX;*.PPTX;*.PPT;*.DOC";
            sfd.FilterIndex = 1;
            System.Windows.Forms.DialogResult rs = sfd.ShowDialog();
            if (rs == System.Windows.Forms.DialogResult.OK)
            {
                MyWindow win=new MyWindow();
                win.Status=false;
                win.IsSelected=false;
                string newpath = App.ProjectDirectory + "\\Documents\\" + System.IO.Path.GetFileName(sfd.FileName);
                System.IO.File.Copy(sfd.FileName, newpath, true);
                ResourcesHelper.GetInstance().AddResource(newpath);
                win.ContentId = ResourcesHelper.GetInstance().PathToID(newpath);
                WindowsHelper.GetInstance().AddWindows(win);
                TreeViewItem item = new TreeViewItem();
                item.Header = System.IO.Path.GetFileName(sfd.FileName);
                item.FontWeight = FontWeights.Normal;
                wpf.ContextMenu cMenu;
                cMenu = new wpf.ContextMenu();
                wpf.MenuItem itemDelete = new wpf.MenuItem();
                itemDelete.Header = "Delete";
                itemDelete.Click += new RoutedEventHandler(itemDelete_Click);
                wpf.Image im = new Image();
                Uri uri = new Uri("pack://application:,,,/UIImage/file_delete.jpg");
                BitmapImage source = new BitmapImage(uri);
                im.Source = source;
                im.Height = 16;
                im.Width = 16;
                itemDelete.Icon = im;
                TreeItemHandler handler = new TreeItemHandler();
                handler.FileName = newpath;
                cMenu.Items.Add(itemDelete);
                cMenu.Tag = handler;
                item.ContextMenu = cMenu;
                TreeItemHandler handle = new TreeItemHandler();
                handle.FileName = newpath;
                handle.DocContent = null;
                item.MouseDoubleClick += new MouseButtonEventHandler(item_DoubleClick);
                item.MouseUp += new MouseButtonEventHandler(item_MouseUp);
                item.Tag = handle;
                //item.Items.Add(dummyNode);
                item.Expanded += new RoutedEventHandler(folder_Expanded);
                foldersItem.Items.Add(item);
            }
        }
        void itemDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                wpf.MenuItem it = sender as wpf.MenuItem;
                wpf.ContextMenu cm = it.Parent as wpf.ContextMenu;
                TreeItemHandler ih = cm.Tag as TreeItemHandler;
                MyWindow win = WindowsHelper.GetWindowsByFileName(ih.FileName);
                WindowsHelper.GetInstance().RemoveWindow(win);
                ResourcesHelper.DeleteResource(ih.FileName);
                System.IO.File.Delete(ih.FileName);
                Prepare_Project_Pane();
            }
            catch
            {
              System.Windows.Forms.MessageBox.Show("Cant't delete item because of using by another process");
            }
        }
        void itemDelete_ClickExportMindmap(object sender, RoutedEventArgs e)
        {
        
                wpf.MenuItem it = sender as wpf.MenuItem;
                wpf.ContextMenu cm = it.Parent as wpf.ContextMenu;
                TreeItemHandler ih = cm.Tag as TreeItemHandler;
                MyWindow win = WindowsHelper.GetWindowsByFileName(ih.FileName);
                WindowsHelper.GetInstance().RemoveWindow(win);
                ResourcesHelper.DeleteResource(ih.FileName);
                System.IO.File.Delete(ih.FileName);
                foreach (MindmapTag tag in App.exportMindmapList)
                {
                    if (tag.path.Equals(ih.FileName))
                    {
                        App.exportMindmapList.Remove(tag);
                        break;
                    }
                }
                Prepare_Project_Pane();
        }

        void itemExport_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.MenuItem item = sender as System.Windows.Controls.MenuItem;
            System.Windows.Controls.ContextMenu menu = item.Parent as System.Windows.Controls.ContextMenu;
            TreeItemHandler itemtag=  menu.Tag as TreeItemHandler;
            string filePath=itemtag.FileName;
            MindMap map = new MindMap(filePath);
            string fileName = System.IO.Path.GetFileName(filePath);
            string name = "[MindMap]-"+fileName+ ".pptx";
            ExportConfig exportForm = new ExportConfig(map.RootNode, name);
            exportForm.ShowDialog();
           
        }


        public void PrepareNodeTree()
        {
            NodeTree.Items.Clear();
            
            //this is for general
            Node n = App.currentMindmap.RootNode;
            TreeViewItemWithIcons item = new TreeViewItemWithIcons();
            item.HeaderText = n.Name;
            item.IsExpanded = n.IsExpanded;
            string packUri = "pack://application:,,,/Images/world.png";
            item.MouseUp+= new MouseButtonEventHandler(item_MouseDoubleClick);
            item.Icon = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;
            TreeViewItemWithIcons.TreeViewTag itemTag = new TreeViewItemWithIcons.TreeViewTag();
            itemTag.node = n;
            itemTag.Doc = null;
            item.Tag = itemTag;
            NodeTree.Items.Add(item);
            TreeViewItemWithIcons.TreeViewTag tag = new TreeViewItemWithIcons.TreeViewTag();
            tag.node = n;
            item.Tag = tag;
            item.projectPane = this;
            AddNode(item, n.ChildrenNodeList);
        }

        void item_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItemWithIcons item=sender as TreeViewItemWithIcons;
            if (item.IsSelected == true)
            {
                TreeViewItemWithIcons.TreeViewTag treeViewTag = item.Tag as TreeViewItemWithIcons.TreeViewTag;
                if (treeViewTag.Doc == null)
                {

                    treeViewTag.Doc = mindmapDoc;
                }

                else
                    App.DockPane.SelectedItem = ((sender as TreeViewItemWithIcons).Tag as TreeViewItemWithIcons.TreeViewTag).Doc;
            }
        }
        void AddNode(TreeViewItemWithIcons treeViewItem, List<Node> listnode)
        {
            foreach (Node n in listnode)
            {
                TreeViewItemWithIcons item = new TreeViewItemWithIcons(n);
                string packUri;
                if (n.ChildrenNodeList.Count == 0)
                {
                    packUri = "pack://application:,,,/Images/leaves.jpg";
                }
                else
                {
                    packUri = "pack://application:,,,/Images/branch.jpg";
                }
                item.Icon = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;
                treeViewItem.Items.Add(item);
                item.projectPane = this;
                if (n.ChildrenNodeList.Count != 0)
                {
                    item.projectPane = this;
                    AddNode(item, n.ChildrenNodeList);
                }
            }
        }
        /// <summary>
        /// Load all parts of Project Pane
        /// </summary>
        public void Prepare_Project_Pane()
        {
            LoadLibrary();
            LoadMindmapFile();
            LoadOutputFile();
        }
        void LoadLibrary()
        {
            foldersItem.Items.Clear();
            //prepare Library
            int count = WindowsHelper.ListWindow.Count;
            for (int i = 0; i < count;i++ )
            {
                SystemFile sysfile = WindowsHelper.ListWindow[i];
                //Init MenuContext
                MyWindow win = sysfile as MyWindow;
                string s = ResourcesHelper.GetInstance().IDToPath(win.ContentId);
                try
                {
                    if ((utility.EqualsExtend(s, "doc") || utility.EqualsExtend(s, "docx") || utility.EqualsExtend(s, "ppt") || utility.EqualsExtend(s, "pptx") || utility.EqualsExtend(s, "pdf") || utility.EqualsExtend(s, "bmp") || utility.EqualsExtend(s, "png") || utility.EqualsExtend(s, "jpg")) && (!s.Contains("~$")))
                    {
                        wpf.ContextMenu cMenu;
                        cMenu = new wpf.ContextMenu();
                        wpf.MenuItem itemDelete = new wpf.MenuItem();
                        itemDelete.Header = "Delete";
                        itemDelete.Click += new RoutedEventHandler(itemDelete_Click);
                        wpf.Image im = new Image();
                        Uri uri = new Uri("pack://application:,,,/UIImage/file_delete.jpg");
                        BitmapImage source = new BitmapImage(uri);
                        im.Source = source;
                        im.Height = 16;
                        im.Width = 16;
                        itemDelete.Icon = im;
                        cMenu.Items.Add(itemDelete);
                        //Add MenuContext
                        TreeViewItem item = new TreeViewItem();
                        item.Header = System.IO.Path.GetFileName(s);
                        TreeItemHandler handle = new TreeItemHandler();
                        handle.FileName = s;
                        handle.DocContent = null;
                        item.Tag = handle;
                        item.FontWeight = FontWeights.Normal;
                        item.ContextMenu = cMenu;
                        cMenu.Tag = handle;
                        item.MouseDoubleClick += new MouseButtonEventHandler(item_DoubleClick);
                        item.MouseUp += new MouseButtonEventHandler(item_MouseUp);
                        //item.MouseEnter += new System.Windows.Input.MouseEventHandler(item_MouseEnter);
                        item.Expanded += new RoutedEventHandler(folder_Expanded);
                        if (foldersItem.Items.Count <= WindowsHelper.ListWindow.Count)
                            foldersItem.Items.Add(item);

                    }
                }
                catch
                {
                    WindowsHelper.ListWindow.Remove(win);
                    i--;
                    count--;
                }
            }
        }
        void LoadMindmapFile()
        {
            //prepare Mindmap File
            stkPanel.Children.Clear();
            foreach (string s in Directory.GetFiles(App.ProjectDirectory + "//Mindmaps"))
            {
                if (!System.IO.Path.GetFileName(s).Equals("mindmap.xml"))
                {
                    MindMapButton button = new MindMapButton();
                    button.Filepath = s;
                    button.Tittle = System.IO.Path.GetFileName(s);
                    stkPanel.Children.Add(button);
                    MindMap mindmap = new MindMap(s);
                    button.MouseDoubleClick += new MouseButtonEventHandler(button_MouseDoubleClick);
                    MindmapTag mindtag = new MindmapTag();
                    mindtag.editor = null;
                    mindtag.mindmap = mindmap;
                    mindtag.path = s;
                    button.Tag = mindtag;
                    wpf.ContextMenu cMenu = new wpf.ContextMenu();
                    wpf.MenuItem itemDelete = new wpf.MenuItem();
                    itemDelete.Header = "Delete";
                    itemDelete.Click += new RoutedEventHandler(itemDelete_ClickExportMindmap);
                    wpf.Image im = new Image();
                    Uri uri = new Uri("pack://application:,,,/UIImage/file_delete.jpg");
                    BitmapImage source = new BitmapImage(uri);
                    im.Source = source;
                    im.Height = 16;
                    im.Width = 16;
                    itemDelete.Icon = im;
                    cMenu.Items.Add(itemDelete);

                    wpf.MenuItem itemExport = new wpf.MenuItem();
                    itemExport.Header = "Export";
                    itemExport.Click +=new RoutedEventHandler(itemExport_Click);
                   Image im2 = new Image();
                   uri = new Uri("pack://application:,,,/Images/export-icon.png");
                    source = new BitmapImage(uri);
                    im2.Source = source;
                    im2.Height = 16;
                    im2.Width = 16;
                    itemExport.Icon = im2;
                    cMenu.Items.Add(itemExport);
                    TreeItemHandler itemhandler = new TreeItemHandler();
                    itemhandler.FileName = s;
                    cMenu.Tag = itemhandler;
                    button.ContextMenu = cMenu;                    
                    if(this.IsGlobal==false)
                        App.exportMindmapList.Add(mindtag);
                }

            }        
        }
        public void LoadOutputFile()
        {
            OutputStk.Children.Clear();
            Uri uri1 = new Uri("pack://application:,,,/Images/ppt.jpg");
            BitmapImage source1 = new BitmapImage(uri1);
            if (Directory.Exists(App.ProjectDirectory + "//Outputs"))
                foreach (string s in Directory.GetFiles(App.ProjectDirectory + "//Outputs"))
                {
                    if (!s.Contains("~$"))
                    {
                        MindMapButton button = new MindMapButton();
                        button.ButtonImageSource = source1;
                        button.Filepath = s;
                        button.Tittle = System.IO.Path.GetFileName(s);
                       
                        button.MouseDoubleClick += new MouseButtonEventHandler(buttonOutput_MouseUp);
                        MindmapTag mindtag = new MindmapTag();
                        wpf.ContextMenu cMenu = new wpf.ContextMenu();
                        wpf.MenuItem itemDelete = new wpf.MenuItem();
                        itemDelete.Header = "Delete";
                        itemDelete.Click += new RoutedEventHandler(itemDelete_Click);
                        wpf.Image im = new Image();
                        Uri uri = new Uri("pack://application:,,,/UIImage/file_delete.jpg");
                        BitmapImage source = new BitmapImage(uri);
                        im.Source = source;
                        im.Height = 16;
                        im.Width = 16;
                        itemDelete.Icon = im;
                        cMenu.Items.Add(itemDelete);
                        TreeItemHandler itemhandler = new TreeItemHandler();
                        itemhandler.FileName = s;
                        cMenu.Tag = itemhandler;
                        button.ContextMenu = cMenu;
                        mindtag.editor = null;
                        mindtag.path = s;
                        button.Tag = mindtag;
                        OutputStk.Children.Add(button);
                    }
                }
            
        }
        void buttonOutput_MouseUp(object sender, MouseButtonEventArgs e)
        {
            MindMapButton btn = sender as MindMapButton;
            MindmapTag mindtag = btn.Tag as MindmapTag;
            if (mindtag.docContent == null || mindtag.docContent.Parent == null)
            {
                mindtag.docContent= DocumentContainForViewer.OpenFile(mindtag.path);
                if (mindtag.docContent == null)
                {
                    System.Windows.MessageBox.Show("Error occurred when open powerpoint file, please try again");
                    return;
                }
                btn.Tag = mindtag;
                App.DockPane.Items.Add(mindtag.docContent);
            }
            App.DockPane.SelectedItem = mindtag.docContent;

        }
        void l_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Background = Brushes.White;
        }

        void l_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Background = Brushes.LightCyan;
        }

        void item_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Background = Brushes.LightCyan;
        }

        void button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MindMapButton btn= sender as MindMapButton;
            string fileName=System.IO.Path.GetFileName(btn.Filepath);
            if (btn.Doc == null||btn.Doc.Parent==null)
            {
                    
                    MindmapEditor editor = new MindmapEditor(true);
                    
                    DocumentContent itemDock = new DocumentContent();
                    itemDock.Closed += new EventHandler(itemDock_Closed);
                    itemDock.Title = "[Mindmap]-"+fileName.Substring(0,fileName.Length-4);
                    itemDock.Content = editor;
                    App.DockPane.Items.Add(itemDock);
                    editor.LoadData((btn.Tag as MindmapTag).mindmap);
                    mindmapDoc = itemDock;
                    this.AddToViewModel(editor.ViewModel);
                    btn.Doc = itemDock;
                    App.observerList.Add(editor);
             }
                App.DockPane.SelectedItem = btn.Doc;
        }
        void item_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            TreeItemHandler handler = item.Tag as TreeItemHandler;
            if (handler.DocContent == null)
            {

                App.dockManager.MainDocumentPane =App.DockPane;
                handler.DocContent = DocumentContainForViewer.OpenFile(handler.FileName);
                if (handler.DocContent != null)
                {
                    item.Tag = handler;
                    App.DockPane.Items.Add(handler.DocContent);
                    App.DockPane.SelectedItem = handler.DocContent;
                    handler.DocContent.Tag = item;
                    handler.DocContent.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(DocContent_Closing);
                }

            }
        }
        void DocContent_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            DocumentContent dc = sender as DocumentContent;
            TreeViewItem tvi = dc.Tag as TreeViewItem;
            TreeItemHandler handler = tvi.Tag as TreeItemHandler;
            handler.DocContent = null;
        }
        void item_MouseUp(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            TreeItemHandler handler = item.Tag as TreeItemHandler;
            if (handler.DocContent != null && handler.DocContent.Parent != null)
            {
                (handler.DocContent.Parent as DocumentPane).SelectedItem = handler.DocContent;
                if (utility.EqualsExtend(System.IO.Path.GetFileName(handler.FileName), "doc") || (utility.EqualsExtend(System.IO.Path.GetFileName(handler.FileName), "docx")))
                {
                    ((handler.DocContent.Content as System.Windows.Forms.Integration.WindowsFormsHost).Child as WordViewer).Reactive();
                }
            }
        }
        private void foldersItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            wpf.TreeView tree = (wpf.TreeView)sender;
            TreeViewItem temp = ((TreeViewItem)tree.SelectedItem);

            if (temp == null)
                return;
            SelectedImagePath = "";
            string temp1 = "";
            string temp2 = "";
            while (true)
            {
                temp1 = temp.Header.ToString();
                if (temp1.Contains(@"\"))
                {
                    temp2 = "";
                }
                SelectedImagePath = temp1 + temp2 + SelectedImagePath;
                if (temp.Parent.GetType().Equals(typeof(wpf.TreeView)))
                {
                    break;
                }
                temp = ((wpf.TreeViewItem)temp.Parent);
                temp2 = @"\";
            }
        }
        void folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == dummyNode)
            {
                item.Items.Clear();
                try
                {
                    foreach (string s in Directory.GetDirectories(item.Tag.ToString()))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = System.IO.Path.GetFileName(s.Substring(s.LastIndexOf("\\") + 1));
                        subitem.Tag = s;
                        subitem.FontWeight = FontWeights.Normal;
                        subitem.Items.Add(dummyNode);
                        subitem.Expanded += new RoutedEventHandler(folder_Expanded);
                        item.Items.Add(subitem);
                    }
                    foreach (string s in Directory.GetFiles(item.Tag.ToString()))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = System.IO.Path.GetFileName(s.Substring(s.LastIndexOf("\\") + 1));
                        subitem.Tag = s;
                        subitem.FontWeight = FontWeights.Normal;
                        //subitem.Items.Add(dummyNode);          
                        item.Items.Add(subitem);
                    }
                }
                catch (Exception) { }
            }
        }
        public void PrepareSystemFile()
        {
            DataLayer.ResourcesHelper.DestinationFile = App.ProjectDirectory + "\\" + "SystemFile" + "\\" + "Resources.xml";
            DataLayer.WindowsHelper.DestinationFile = App.ProjectDirectory + "\\" + "workspace.xml";
            DataLayer.ResourcesHelper rh = ResourcesHelper.GetInstance();
            rh.LoadAll();
            DataLayer.WindowsHelper wh = WindowsHelper.GetInstance();
            
            List<SystemFile> listWin = wh.LoadAll();
            //re-load previous tab
            if (listWin != null)
            {
                foreach (SystemFile s in listWin)
                {
                    MyWindow myWin = s as MyWindow;
                    string fileName = rh.IDToPath(myWin.ContentId);
                    if (fileName != null && System.IO.File.Exists(fileName)&&myWin.Status==true)
                    {

                        DocumentContainForViewer docContent = DocumentContainForViewer.OpenFile(fileName);

                        if (myWin.IsSelected)
                        {
                            App.DockPane.SelectedItem = (docContent);
                        }
                        docContent.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(DocContent_Closing);
                        App.DockPane.Items.Add(docContent);
                        for (int i = 0; i < foldersItem.Items.Count; i++)
                        {
                            TreeItemHandler handler = (foldersItem.Items[i] as TreeViewItem).Tag as TreeItemHandler;
                            if (handler.FileName.Equals(fileName))
                            {

                                handler.DocContent = docContent;
                                handler.DocContent.Tag = foldersItem.Items[i];
                                (foldersItem.Items[i] as TreeViewItem).Tag = handler;
                            }
                        }
                    }
                }
            }
            RestoreLayout(null, null);
        }
        private void SaveLayout(object sender, RoutedEventArgs e)
        {
            App.dockManager.SaveLayout(App.ProjectDirectory + "\\" + "SystemFile" + "\\" + "layout.xml");
        }

        private void RestoreLayout(object sender, RoutedEventArgs e)
        {
            try
            {
                if (File.Exists(App.ProjectDirectory + "//" + "SystemFile" + "\\" + "layout.xml"))
                {
                    App.dockManager.RestoreLayout(App.ProjectDirectory + "\\" + "SystemFile" + "\\" + "layout.xml");
                }
            }
            catch (Exception err)
            {

            }

        }
        public void AddToViewModel(MainWindowViewModel viewmodel)
        {
            viewmodel.AddObserver(this);
        }
        DocumentContent mindmapDoc;
        private void Step2_Expanded(object sender, RoutedEventArgs e)
        {
            
            
            if (editor == null)
            {
                editor = new MindmapEditor();
                DocumentContent itemDock = new DocumentContent();
                itemDock.Closed += new EventHandler(itemDock_Closed);
                itemDock.Title = "[Mindmap]-General";
                itemDock.Content = editor;
                App.DockPane.Items.Add(itemDock);
                editor.LoadData(App.currentMindmap);
                mindmapDoc = itemDock;
                this.AddToViewModel(editor.ViewModel);
                App.mainMindmapEditor = editor;
                App.observerList.Add(editor);
                App.observerList.Add(this);
            }
            App.DockPane.SelectedItem=editor.Parent as DocumentContent;

        }

        void itemDock_Closed(object sender, EventArgs e)
        {
            editor = null;
        }

        private void Step1_Expanded(object sender, RoutedEventArgs e)
        {
            if(Step2!=null)
            Step2.IsExpanded = false;
        }

        private void NodeTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                TreeViewItemWithIcons item= sender as TreeViewItemWithIcons;
                if (item.IsSelected == true)
                {
                    App.DetailView.SetContain(((TreeViewItemWithIcons)e.NewValue).Tag as Node);
                }
            }
            catch(Exception ex)
            {
            
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Add_File_Event(null, null);
        }

        private void AddMindMap(object sender, RoutedEventArgs e)
        {

        }

        private void AddMouseUp(object sender, MouseButtonEventArgs e)
        {
            Add_File_Event(null, null);
        }

        private void StackPanel_Drop(object sender, System.Windows.DragEventArgs e)
        {
            if (e.Data.GetDataPresent("FileDrop"))
            {
                string path = (e.Data.GetData("FileDrop") as string[])[0];
                MyWindow win = new MyWindow();
                win.Status = false;
                win.IsSelected = false;
                string newpath = App.ProjectDirectory + "\\Documents\\" + System.IO.Path.GetFileName(path);
                System.IO.File.Copy(path, newpath, true);
                ResourcesHelper.GetInstance().AddResource(newpath);
                win.ContentId = ResourcesHelper.GetInstance().PathToID(newpath);
                WindowsHelper.GetInstance().AddWindows(win);
                TreeViewItem item = new TreeViewItem();
                item.Header = System.IO.Path.GetFileName(path);
                item.FontWeight = FontWeights.Normal;
                wpf.ContextMenu cMenu;
                cMenu = new wpf.ContextMenu();
                wpf.MenuItem itemDelete = new wpf.MenuItem();
                itemDelete.Header = "Delete";
                itemDelete.Click += new RoutedEventHandler(itemDelete_Click);
                wpf.Image im = new Image();
                Uri uri = new Uri("pack://application:,,,/UIImage/file_delete.jpg");
                BitmapImage source = new BitmapImage(uri);
                im.Source = source;
                im.Height = 16;
                im.Width = 16;
                itemDelete.Icon = im;
                cMenu.Items.Add(itemDelete);
                cMenu.Tag = item;
                item.ContextMenu = cMenu;
                TreeItemHandler handle = new TreeItemHandler();
                handle.FileName = newpath;
                handle.DocContent = null;
                item.MouseDoubleClick += new MouseButtonEventHandler(item_DoubleClick);
                item.MouseUp += new MouseButtonEventHandler(item_MouseUp);
                item.Tag = handle;
                //item.Items.Add(dummyNode);
                item.Expanded += new RoutedEventHandler(folder_Expanded);
                foldersItem.Items.Add(item);
                
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            AddMouseUp(null, null);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            AddNewMindmap mindmapAdd = new AddNewMindmap();
            mindmapAdd.ShowDialog();
            if (mindmapAdd.result)
            {
                string path = App.ProjectDirectory + "/Mindmaps/" + AddNewMindmap.name + ".xml";
                //System.IO.File.Create(path);
                MindMap mindmap = new MindMap(App.currentMindmap.RootNode);
               
                mindmap.MindmapToXmlDoc(path);
                MindMapButton button = new MindMapButton();
                MindmapTag mindtag = new MindmapTag();
                mindtag.path = path;
                mindtag.mindmap = mindmap;
                mindtag.editor = null;
                button.Tag = mindtag;
                //
                 wpf.ContextMenu cMenu = new wpf.ContextMenu();
                wpf.MenuItem itemDelete = new wpf.MenuItem();
                itemDelete.Header = "Delete";
                itemDelete.Click += new RoutedEventHandler(itemDelete_ClickExportMindmap);
                wpf.Image im = new Image();
                Uri uri = new Uri("pack://application:,,,/UIImage/file_delete.jpg");
                BitmapImage source = new BitmapImage(uri);
                im.Source = source;
                im.Height = 16;
                im.Width = 16;
                itemDelete.Icon = im;
                cMenu.Items.Add(itemDelete);
                TreeItemHandler itemhandler = new TreeItemHandler();
                itemhandler.FileName = path;
                cMenu.Tag = itemhandler;
                button.ContextMenu = cMenu;
                //
                button.Filepath = path;
                button.Tittle = System.IO.Path.GetFileName(path);
                stkPanel.Children.Add(button);
                button.MouseDoubleClick += new MouseButtonEventHandler(button_MouseDoubleClick);
                App.exportMindmapList.Add(mindtag);
            }
        }

    }
}
